package org.aeon.gamechatnest.service.impl;

import org.aeon.gamechatnest.common.response.ResultBuild;
import org.aeon.gamechatnest.exception.TargetIsAnEmptyException;
import org.aeon.gamechatnest.mapper.TextChannelMapper;
import org.aeon.gamechatnest.pojo.TextChannel;
import org.aeon.gamechatnest.service.TextChannelService;
import org.aeon.gamechatnest.service.UserServerService;
import org.aeon.gamechatnest.util.ApplicationAssert;
import org.aeon.gamechatnest.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : Liys 2024/5/11 09点38分
 */
@Service
public class TextChannelServiceImpl implements TextChannelService {

    @Autowired
    private TextChannelMapper textChannelMapper;

    @Lazy
    @Autowired
    private UserServerService userServerService;


    /**
     * 新增文字频道
     *
     * @param textChannel
     * @return
     */
    @Override
    public boolean save(TextChannel textChannel) {
        if (userServerService.isNotExist(textChannel.getServerId())) {
            ResultBuild.result().message("服务器不存在");
        }
        return textChannelMapper.add(textChannel) > 0;
    }

    /**
     * 通过频道id删除频道
     *
     * @param tcId
     * @return
     */
    @Override
    @Transactional
    public boolean removeByTcId(Long tcId) {
        if (isNotExist(tcId)) {
            ResultBuild.result().message("频道不存在");
            return false;
        }
        // TODO 删除频道所有消息
        // TODO 删除频道所有额外权限
        return textChannelMapper.deleteByTcId(tcId) > 0;
    }

    /**
     * 通过频道id修改频道
     *
     * @param textChannel
     * @return
     */
    @Override
    public boolean modifyByTcId(TextChannel textChannel) {
        if (isNotExist(textChannel.getTcId())) {
            ResultBuild.result().message("频道不存在");
            return false;
        }
        return textChannelMapper.updateByTcId(textChannel) > 0;
    }

    /**
     * 通过频道名称模糊查询频道
     *
     * @param name
     * @return
     */
    @Override
    public List<TextChannel> findByName(String name) {
        return textChannelMapper.getByName(name);
    }

    /**
     * 通过频道id查询频道
     *
     * @param tcId
     * @return
     */
    @Override
    public TextChannel findByTcId(Long tcId) {
        return textChannelMapper.getByTcId(tcId);
    }

    @Override
    public boolean isNotExist(Long tcId) {
        return !textChannelMapper.isExist(tcId);
    }

    @Override
    public boolean createDefaultChannel(Long serverId) {
        // TODO 服务器创建频道模板
        TextChannel defaultChannel1 = new TextChannel(null, serverId, "默认频道", null);
        return save(defaultChannel1);
    }

    @Override
    public Long getServerIdByChannelId(Long tcId) throws TargetIsAnEmptyException {
        Long serverId = textChannelMapper.getServerIdByChannelId(tcId);
        ApplicationAssert.nonNull(serverId, "服务器不存在");
        return serverId;
    }

    @Override
    public boolean removeByServerId(Long serverId) {
        textChannelMapper.deleteByServerId(serverId);
        return true;
    }

    @Override
    public List<TextChannel> textChannels() {
        Long server = SecurityUtil.getServer();
        ApplicationAssert.nonNull(server, "服务器不存在");
        ApplicationAssert.nonNull(userServerService.isExist(server), "服务器不存在");
        return textChannelMapper.getByServerId(server);
    }
}
